package gui;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.*;
import lang.LOC;

/**
 * Diese Klasse erweitert ein JPanel und wird fuer
 * schliessbare Tabkomponenten im oberen Bereich
 * des Programms genutzt.
 */

public class ClosableTabComponent extends JPanel {
    
    private final JTabbedPane pane;
    private final JLabel label;
    private TabButton button = null;
    
    public ClosableTabComponent(final Layout tt, final JTabbedPane pane, final Icon icon) {
        this(tt, pane);
        this.label.setIcon(icon);
    }
    
    public ClosableTabComponent(final Layout tt, final JTabbedPane pane) {
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        if (pane == null) {
            throw new NullPointerException("Layout or Pane is NULL");
        }
        button = new TabButton(tt);
        this.pane = pane;
        setOpaque(false);
        
        JLabel label = new JLabel() {
            public String getText() {
                int i = pane.indexOfTabComponent(ClosableTabComponent.this);
                if (i != -1) {
                    return pane.getTitleAt(i);
                }
                return null;
            }
        };
        this.label = label;
        setFocusable(false);                
        
        add(label);
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 3));
        Dimension fixedSizeLabel = new Dimension(152,16);
        label.setPreferredSize(fixedSizeLabel);
        label.setMaximumSize(fixedSizeLabel);
        label.setFocusable(false);
        
        add(button);
        Dimension fixedSize = new Dimension(170,16);
        this.setPreferredSize(fixedSize);
        this.setMaximumSize(fixedSize);
    }
    
    public void setInActiv() {
        Font font = new Font(label.getFont().getName(),Font.PLAIN,label.getFont().getSize()); 
        label.setFont(font);
    }
    public void setActiv() {
        Font font = new Font(label.getFont().getName(),Font.BOLD,label.getFont().getSize()); 
        label.setFont(font);
    }

    private class TabButton extends JButton implements ActionListener {
        private Layout tt;
        
        public TabButton(Layout tt) {
            this.tt = tt;
            int size = 12;
            setPreferredSize(new Dimension(size, size));
            setToolTipText(LOC.get("closeTab"));
            setUI(new BasicButtonUI());
            setContentAreaFilled(false);
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder(1));
            setBorderPainted(false);
            addMouseListener(buttonMouseListener);
            setRolloverEnabled(true);
            addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            int i = pane.indexOfTabComponent(ClosableTabComponent.this);
            if (i != -1) {
                ScaProject p = tt.getProjectAt(i);
                if(p == null) {
                   MyTabbedPane tmp2 = (MyTabbedPane)MyTabbedPane.wrap(pane);
                    tmp2.removeTab(i); 
                }
                else {
                    tt.closeProject(p);
                }
                if(i > 0) {
                    TabbedPaneClosable tmp = (TabbedPaneClosable)TabbedPaneClosable.wrap(pane);
                    tmp.setSelectedIndex(pane.getSelectedIndex());
                }
            }
        }

        public void updateUI() {
        }

        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            if (getModel().isPressed()) {
                g2.translate(1, 1);
            }
            g2.setStroke(new BasicStroke(1));
            g2.setColor(Color.BLACK);
            if (getModel().isRollover()) {
                g2.setColor(Color.GRAY);
            }
            int delta = 3;
            g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
            g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
            g2.dispose();
        }
    }

    private final static MouseListener buttonMouseListener = new MouseAdapter() {
        public void mouseEntered(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(true);
            }
        }

        public void mouseExited(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(false);
            }
        }
    };
    
}


 